package com.fuhui.controller.service;

import com.fuhui.dao.txm.AppconfigApplistMapper;
import com.fuhui.dao.txm.AppconfigMapper;
import com.fuhui.entity.txm.Appconfig;
import com.fuhui.entity.txm.AppconfigApplist;
import com.fuhui.util.DateFromat;
import com.fuhui.util.ReturnJson;
import com.fuhui.util.SnowFlake;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class SchoolAppConfigService {

    @Autowired
    private AppconfigMapper appconfigMapper;

    @Autowired
    private AppconfigApplistMapper appconfigApplistMapper;

    /**
     *
     * @param request
     * @return
     */
    @RequestMapping("saveConfig")
    public Object saveConfig(HttpServletRequest request){
        Map<String,Object> map = new HashMap<String, Object>();
        SnowFlake snowFlake = new SnowFlake(2, 3);
        Date date = new Date();
        String idDel = "0";
        long iConfigid = snowFlake.nextId();
        long I_IDX = snowFlake.nextId();
        Appconfig appconfig = new Appconfig();
        try {
            String name = request.getParameter("name");
            long schoolId = Long.valueOf(request.getParameter("schoolId"));
            Integer range = Integer.parseInt(request.getParameter("range"));
            String rangeIds = request.getParameter("rangeIds");
            String remark = request.getParameter("remark");
            Integer status = Integer.parseInt(request.getParameter("status"));

            appconfig.setiConfigid(iConfigid);
            appconfig.setiSchoolid(schoolId);
            appconfig.setcName(name);
            appconfig.setcRangeids(rangeIds);
            appconfig.setcRemark(remark);
            appconfig.settCtime(DateFromat.datetimeChange(date));
            appconfig.settUtime(DateFromat.datetimeChange(date));
            appconfig.setiCuserid(iConfigid);//操作人id登录后取值（创建人）
            appconfig.setiUuserid(iConfigid);//操作人id登录后取值（更新人）
            appconfig.setiRange(range);
            appconfig.setiStatus(status);
        }catch (Exception e){
            System.out.println("提交参数异常！");
        }

        int result = appconfigMapper.add(appconfig);
        if (result > 0 ){
            map.put("iConfigid",iConfigid);
            AppconfigApplist txmApplist = new AppconfigApplist();
            txmApplist.setiConfigid(iConfigid);
            txmApplist.setiIdx(I_IDX);
            txmApplist.setiAppid(iConfigid);
            txmApplist.setiCuserid(iConfigid);
            Date txmdate = new Date();
            txmApplist.settCtime(DateFromat.datetimeChange(txmdate));
            int jg = saveAppList(idDel,txmApplist);
            if(jg > 0){
                return ReturnJson.returnSuccessMap(map);
            }
        }
        return ReturnJson.returnFail();
    }

    private int saveAppList(String isDel,AppconfigApplist appconfigApplist){
        //isDel用于判断是否删除T_XM_AppConfig_AppListLog中的数据
        /*if (isDel == "1" || isDel.equals("1") ){
        }else {
        }*/
        int k = appconfigApplistMapper.saveAppList(appconfigApplist);
        if (k > 0){
            return 1;
        }else {
            return 0;
        }
    }
}
